<?php
defined('_JEXEC') or die;
$this->document->setTitle("Elev profil");
$tpath	= JURI::base().'templates/'.JFactory::$application->getTemplate();
$id		=JRequest::getInt("user_id",0);

$m_start=intval(substr($this->data->registerDate,5,2));
$y_start=intval(substr($this->data->registerDate,0,4));
$m_now=intval(date("m"));
$y_now=intval(date("Y"));
if($y_start!=$y_now)
	$m_now+=12;
$map=array(0,'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December',
'Januar','Februar','Marts','April','Maj','Juni',
'Juli','August','September','Oktober','November','December');
$period= 12;//$this->data->information['length']==1 ? 12 : $this->data->information['length'];

	function mySql2PhpTime($sqlDate){
		$arr = date_parse($sqlDate);
		return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);
	}

$db=JFactory::getDbo();
$db->setQuery("SELECT * FROM #__student_month WHERE id_user=".$this->data->id." ORDER BY id");
$rows=$db->loadObjectList();

//count items
$db->setQuery("SELECT registerDate FROM `#__users` WHERE id=".$this->data->id);
$regDate=$db->loadResult();$regDate=mySql2PhpTime($regDate);
$regYear=(int)date("y", $regDate);
$regDate=(int)date("W", $regDate);//regDate become regWeek

$db->setQuery("SELECT id,StartTime,EndTime,Color,RecurringRule FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=0");
$items=$db->loadObjectList();
$yes=0;$no=0;
foreach($items as $item){

	if($item->Color==10)
		$yes++;
	else if($item->Color==2)
		$no++;
//count real items

	if($item->RecurringRule){

		$item->RecurringRule=json_decode($item->RecurringRule);

		$sDate=mySql2PhpTime($item->StartTime);
		$sYear=(int)date("y", $sDate);
		$sWeek=(int)date("W", $sDate);

		$weeks=$item->RecurringRule->weeks;
		$weekdays=$item->RecurringRule->weekdays;

		if($regYear != $sYear)//is the same year?
			$sWeek=52+$sWeek;

		if(isset($item->RecurringRule->data)){

			$tmp=0;$tmp1=0;
			foreach($item->RecurringRule->data as $dat)
				foreach($dat as $dat0)
					if($dat0==10)
						$tmp1++;
					else if($dat0==2)
						$tmp++;

			$yes+=$tmp1;$no+=$tmp;
		}
	}
}///count
?>
	<link href="<?php echo$tpath?>/calendar/css/dailog.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/calendar.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/dp.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/alert.css" rel="stylesheet" type="text/css" />
	<link href="<?php echo$tpath?>/calendar/css/main.css" rel="stylesheet" type="text/css" />
<div class="wrapper">
	<div class="content clearfix" id="myprofile-page">
		<h1 class="hd">Personlig profil</h1>
		<div class="info-pf">
			<div class="w320 fl">
				<span>Profil-nr. <?php echo $this->data->id;?></span>
				<span>Navn: <?php echo $this->data->name;?></span>
				<span>Startvægt: <?php echo $this->data->Vægt;?> kg.</span>
			</div>
			<div class="w320 fl">
<?php
$db->setQuery("SELECT StartTime FROM #__jqcalendar WHERE id_student=".$this->data->id." AND type=1");
$tmp=$db->loadResultArray();
$now=time();

foreach($tmp as $v){
	$v=mySql2PhpTime($v);
	if($v && $v >= time()){
		$d=date("d.m.Y", $v);
		$tmp=$d. " Kl. " .date("H.i", $v);
		break;
	}
}
if(is_array($tmp))
	$tmp="";
$u=JFactory::getUser();
?>
				<span>Personlig vejleder: <?php echo $u->name?></span>
				<span>Næste webmøde: <?php echo $tmp?></span>
				<!--span>Køb mere tid hos din vejleder <a href="#">her</a></span-->
				<span><a href="<?php echo JRoute::_("index.php?option=com_users&view=profile&layout=student&user_id=".$this->data->id)?>" style="color:blue">Rediger brugeroplysninger</a></span>
			</div>
		</div>
		<div class="clear"></div>

<div id="page-wrap">
	<div id="tabs">
		<ul>
<?php //Month tabs
	for($i=$m_start,$j=0;$j<$period;$j++,$i++){?>
			<li class="ui-state-default ui-corner-top uitab<?php echo $i;if($i==$m_now) echo ' ui-tabs-selected'?>"><a <?php if($i<=$m_now) echo 'onclick="formdisplay('.$i.')"'?>><?php echo $map[$i]?></a></li>
<?php
	}?>
		</ul>

<?php //Content boxes
	for($i=$m_start,$j=0;$i<=$m_now;$j++,$i++){?>
		<div id="fragment-<?php echo $i?>" class="ui-tabs-panel" style="<?php if($i!=$m_now) echo 'display:none'?>">

<?php
$tmp=explode(";", $rows[$j]->event);
if(isset($w[0])) $lastw= $w[0];
?>
			<div class="result clearfix">
					<div class="fl w185">
						<div class="avata-sub">
<?php //auto fit box
if($rows[$j]->image)
	$rows[$j]->image='images/student/'.$rows[$j]->image;
$h='';
if(file_exists($rows[$j]->image))
{
	list( $w, $h)=getimagesize($rows[$j]->image);
	$h=$h > $w ? 'height="240"' : $h='width="185"';
}
?>
				<img <?php echo $h?> <?php echo 'src="'.JURI::root(),$rows[$j]->image,'"'?> alt="portrait (size 185x240)">
						</div>
					</div>
<?php if($i==$m_now){?>
					<div style="margin-top: 250px;position: absolute;margin-left: 39px;"><form method="post" enctype="multipart/form-data">
					<img src="<?php echo $tpath?>/src/images/btn_Dietplan.png" width="106" height="24" alt="" onclick="jQuery('#fileimg').click()" style="cursor: pointer<?php if(isset($rows[0]->file) && strpos($rows[0]->file,'kost')!==FALSE) echo ';border:1px dashed green'?>" />
					<input type="file" onchange="if(this.value) this.form.submit()" id="fileimg" style="display: none" name="stufile" />
					<br />
					<img src="<?php echo $tpath?>/src/images/btn_Shopping.png" width="106" height="24" alt="" onclick="jQuery('#fileimg1').click()" style="cursor: pointer;margin-top:5px<?php if(isset($rows[0]->file) && strpos($rows[0]->file,'indkob')!==FALSE) echo ';border:1px dashed green'?>" />
					<input type="file" onchange="if(this.value) this.form.submit()" id="fileimg1" style="display: none" name="stufile1" />
	<input type="hidden" value="<?php echo $this->data->id?>" name="stud"/>
	<input type="hidden" value="com_users" name="option"/>
	<input type="hidden" value="user.upload_student_file" name="task"/>
					</form></div>
<?php }?>
					<div class="fr w703">
						<div class="fl" style="width:264px">
<?php //statistic data
$w=array_map(function($item){$item=explode(":",$item);return $item[1];}, $tmp);
?>
<p>Vægt:<a class="bntkg"><?php echo $w[0] | 0;?> kg</a></p>
<p>Talje:<a class="bntkg"><?php echo $w[1] | 0?> cm</a></p>
<p>Hofte:<a class="bntkg"><?php echo $w[2] | 0?> cm</a></p>
<p>Bryst:<a class="bntkg"><?php echo $w[3] | 0?> cm</a></p>
<p>Overarm:<a class="bntkg"><?php echo $w[4] | 0?> cm</a></p>
<p class="mt40 mb5">Resultat af tjekplan:</p>
<p class="green mb5">JA (<?php echo $yes?>)</p>
<p class="red mb5">NEJ (<?php echo $no?>)</p>
						</div>

						<div class="fl ml80" style="width:345px">
							<p>Kg. tilbage til slutmålet: <span><?php echo !empty($w[0]) ? $this->data->information['weightdown']-$this->data->Vægt+$w[0] : '-'//how much weight remain?> kg</span></p>
							<p>Tabt siden start: <span><?php echo !empty($w[0]) ? $this->data->Vægt-$w[0] : '-';//offset compare to goal?> kg</span></p>
							<p>Tabt siden sidste måned: <span><?php echo $lastw && !empty($w[0]) ? $lastw-$w[0] : '-'//offset compare to last month?> kg</span></p>
<form method="post" action="" id="f_month<?php echo $i;?>" enctype="multipart/form-data">
							<label style="font-weight: 700">Noter</label>
							<div style="color:#909090"><textarea name="note"><?php
								echo !empty($rows[$j]->note) ? $rows[$j]->note : "Uge 1:

Uge 2:

Uge 3:

Uge 4:";
								?></textarea></div>
								<a class="fl bntSave" onclick="jQuery('#f_month<?php echo $i?>').submit();">Gem</a>
	<input type="hidden" value="<?php echo $i?>" name="month"/>
	<input type="hidden" value="com_users" name="option"/>
	<input type="hidden" value="user.save_student_note" name="task"/>
	<input type="hidden" value="<?php echo $this->data->id?>" name="id_user"/>
</form>
						</div>
					</div>

					<div class="clear"></div>

					<div class="status">
<?php if($i!=$m_start){?>
				<a class="bntPrevious2 fl ml425" rel="<?php echo $i-1?>" onclick="formdisplay(this.rel)">Forrige</a>
<?php }
	if($i<$m_now){?>
				<a class="bntNext2 fr" rel="<?php echo $i+1?>" onclick="formdisplay(this.rel)">Næste</a>
<?php }?>
					</div>
				</div>

		</div>
<?php
	}?>
	</div>
</div>

<div class="calendar mt20">
	<div>
	
		<div id="calhead" style="padding-left:1px;padding-right:1px;">
			<div class="cHead"><div class="ftitle">Notifications</div>
			<div id="loadingpannel" class="ptogtitle loadicon" style="display: none;">Loading data...</div>
			 <div id="errorpannel" class="ptogtitle loaderror" style="display: none;">Sorry, could not load your data, please try again later</div>
			</div>

			<div id="caltoolbar" class="ctoolbar">
				<div id="faddbtn" class="fbutton">
				<div><span title='Click to Create New Event' class="addcal">

				New Event
				</span></div>
			</div>
			<div class="btnseparator"></div>
			 <div id="showtodaybtn" class="fbutton">
				<div><span title='Click to back to today ' class="showtoday">
				Today</span></div>
			</div>
				<div class="btnseparator"></div>

			<div id="showdaybtn" class="fbutton">
				<div><span title='Day' class="showdayview">Day</span></div>
			</div>
				<div	id="showweekbtn" class="fbutton fcurrent">
				<div><span title='Week' class="showweekview">Week</span></div>
			</div>
				<div id="showmonthbtn" class="fbutton">
				<div><span title='Month' class="showmonthview">Month</span></div>

			</div>
			<div class="btnseparator"></div>
				<div id="showreflashbtn" class="fbutton">
				<div><span title='Refresh view' class="showdayflash">Refresh</span></div>
				</div>
			 <div class="btnseparator"></div>
			<div id="sfprevbtn" title="Prev" class="fbutton">
				<span class="fprev"></span>

			</div>
			<div id="sfnextbtn" title="Next" class="fbutton">
				<span class="fnext"></span>
			</div>
			<div class="fshowdatep fbutton">
					<div>
						<input type="hidden" name="txtshow" id="hdtxtshow" />
						<span id="txtdatetimeshow">Loading</span>

					</div>
			</div>

			<div class="clear"></div>
			</div>
		</div>
		<div style="padding:1px;">

		<div class="t1 chromeColor">
			&nbsp;</div>
		<div class="t2 chromeColor">
			&nbsp;</div>
		<div id="dvCalMain" class="calmain printborder">
			<div id="gridcontainer" style="overflow-y: visible;">
			</div>
		</div>
		<div class="t2 chromeColor">

			&nbsp;</div>
		<div class="t1 chromeColor">
			&nbsp;
		</div>
		</div>

	</div>
</div>

	<script src="<?php echo$tpath?>/calendar/src/Plugins/common.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/datepicker_lang_US.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.datepicker.js" type="text/javascript"></script>

	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.alert.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.ifrmdailog.js" defer="defer" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/wdCalendar_lang_US.js" type="text/javascript"></script>
	<script src="<?php echo$tpath?>/calendar/src/Plugins/jquery.calendar.js" type="text/javascript"></script>
<script type="text/javascript">
	$(document).ready(function(){
		var view="week";

		var DATA_FEED_URL = "<?php echo JURI::base()?>index.php?option=com_datafeed&no_html=1";
		var op = {
			view: view,
			theme:0,
			showday: new Date(),
			EditCmdhandler:Edit,
			DeleteCmdhandler:Delete,
			ViewCmdhandler:View,
			onWeekOrMonthToDay:wtd,
			onBeforeRequestData: cal_beforerequest,
			onAfterRequestData: cal_afterrequest,
			onRequestDataError: cal_onerror,
			autoload:true,
			url: DATA_FEED_URL + "&method=list&id=<?php echo $id?>",
			quickAddUrl: DATA_FEED_URL + "&method=add&&id=<?php echo $id?>",
			quickUpdateUrl: DATA_FEED_URL + "&method=update",
			quickDeleteUrl: DATA_FEED_URL + "&method=remove",
			enableDrag: false
		};
		var $dv = $("#calhead");
		var _MH = document.documentElement.clientHeight;
		var dvH = $dv.height() + 2;
		op.height = _MH - dvH;
		op.eventItems =[];

		var p = $("#gridcontainer").bcalendar(op).BcalGetOp();

		if (p && p.datestrshow) {
			$("#txtdatetimeshow").text(p.datestrshow);
		}
		$("#caltoolbar").noSelect();

		$("#hdtxtshow").datepicker({ picker: "#txtdatetimeshow", showtarget: $("#txtdatetimeshow"),
		onReturn:function(r){
				var p = $("#gridcontainer").gotoDate(r).BcalGetOp();
				if (p && p.datestrshow) {
					$("#txtdatetimeshow").text(p.datestrshow);
				}
		}
		});
		function cal_beforerequest(type)
		{
			var t="Loading data...";
			switch(type)
			{
				case 1:
				t="Loading data...";
				break;
				case 2:
				case 3:
				case 4:
				t="The request is being processed ...";
				break;
			}
			$("#errorpannel").hide();
			$("#loadingpannel").html(t).show();
		}
		function cal_afterrequest(type)
		{
			switch(type)
			{
				case 1:
					$("#loadingpannel").hide();
					break;
				case 2:
				case 3:
				case 4:
					$("#loadingpannel").html("Success!");
					window.setTimeout(function(){ $("#loadingpannel").hide();},2000);
				break;
			}

		}
		function cal_onerror(type,data)
		{
			$("#errorpannel").show();
		}
		function Edit(data)
		{
			var eurl=DATA_FEED_URL+"&method=edit&id={0}&start={2}&end={3}&isallday={4}&title={1}&user_id=<?php echo $id?>";
			if(data)
			{
				var url = StrFormat(eurl,data);
				OpenModelWindow(url,{ width: 600, height: 376, caption:"Edit event", onclose:function(){
					$("#gridcontainer").reload();//reload the calendar
				}});
			}
		}
		function View(data)
		{
			/*var str = "";
			$.each(data, function(i, item){
				str += "[" + i + "]: " + item + "\n";
			});
			alert(str);*/
		}
		function Delete(data,callback)
		{

			$.alerts.okButton="Ok";
			$.alerts.cancelButton="Cancel";
			hiConfirm("Are You Sure to Delete this Event", 'Confirm',function(r){r && callback(0)});
		}
		function wtd(p)
		{
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$("#showdaybtn").addClass("fcurrent");
		}
		//to show day view
		$("#showdaybtn").click(function(e) {
			//document.location.href="#day";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("day").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});
		//to show week view
		$("#showweekbtn").click(function(e) {
			//document.location.href="#week";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("week").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}

		});
		//to show month view
		$("#showmonthbtn").click(function(e) {
			//document.location.href="#month";
			$("#caltoolbar div.fcurrent").each(function() {
				$(this).removeClass("fcurrent");
			})
			$(this).addClass("fcurrent");
			var p = $("#gridcontainer").swtichView("month").BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});

		$("#showreflashbtn").click(function(e){
			$("#gridcontainer").reload();
		});

		//Add a new event
		$("#faddbtn").click(function(e) {
			var url ="edit.php";
			OpenModelWindow(url,{ width: 500, height: 406, caption: "Create New Calendar"});
		});
		//go to today
		$("#showtodaybtn").click(function(e) {
			var p = $("#gridcontainer").gotoDate().BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}


		});
		//previous date range
		$("#sfprevbtn").click(function(e) {
			var p = $("#gridcontainer").previousRange().BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}

		});
		//next date range
		$("#sfnextbtn").click(function(e) {
			var p = $("#gridcontainer").nextRange().BcalGetOp();
			if (p && p.datestrshow) {
				$("#txtdatetimeshow").text(p.datestrshow);
			}
		});

	});
</script>
	</div>
</div>
<script type="text/javascript">
var cur=<?php echo $m_now?>;
var w_1=0;
	function formdisplay(x){
		pre1='fragment-';
		pre2='uitab';
		if(cur!=x){
			jQuery("#"+pre1+cur).hide();
			jQuery("."+pre2+cur).removeClass('ui-tabs-selected');
		}
		jQuery("#"+pre1+x).show();
		jQuery("."+pre2+x).addClass('ui-tabs-selected');
		cur=x;
	}
</script>
<style type="text/css">
	.resizer{display:none !important}
</style>